<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Pensum extends MN_Controller {

    function __construct() {
        parent::__construct();
        $this->load->database();
        $this->load->helper(array('url', 'form'));
        $this->load->library('ion_auth');
        $this->load->library('session');
        $this->load->model('Pensum_Model');
        $this->load->model('Carrera_Model');
    }

    public function index() {

      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary() )  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
    }

    public function inicio() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary() )  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->data['pensum'] = $this->Pensum_Model->getListado();
        $this->_render('pensum/start');
    }

    public function crear() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary() )  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->library('form_validation');
        //print_r($this->data['institucion']);
        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $this->_render('pensum/new');

        //redirect('auth', 'refresh');
    }

    public function actualizar() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $this->load->helper(array('url', 'form'));
        $this->load->library('form_validation');
        $this->data['carrera'] = $this->Carrera_Model->getListado();

        //$this->data['institucion']=$this->Institucion_Model->getDatosInstitucion($ide);
        $opc = $this->uri->segment(3);
        $ide = $this->uri->segment(4);
        if (!isset($_POST['txtdes'])) {
            $this->data['pensum'] = $this->Pensum_Model->getDatosPensum($ide);
            $this->data['opc'] = $opc;
            $this->_render('pensum/update');
        } else {
            $ide = $this->input->post('txtide');
            $opc = $this->input->post('txtopc');
            $this->data['pensum'] = $this->Pensum_Model->getDatosPensum($ide);
            $this->data['opc'] = $opc;

            $this->form_validation->set_rules('txtdes', 'Descripcion del Pensum', 'required|trim');

            if (($this->form_validation->run() == FALSE)) {
                $this->_render('pensum/update');
                //redirect("institucion/actualizar/".$opc."/".$ide);
            } else {
                $pensum = array(
                    "ide_car" => $this->input->post('ide_car'),
                    "descripcion_pen" => $this->input->post('txtdes'),
                    "estado_pen" => $this->input->post('estado'));

                
                    if ($opc == 'upd'){
                        $this->Pensum_Model->actualizarPensum($pensum, $ide);
                        redirect('pensum/inicio');
                    }
                        
                    else{
                        $msj_del="";
                        if ($this->Pensum_Model->ExisteActividadCiclos($ide)){
                           $msj_del = "<span>NO SE PUEDE ELIMINAR ESTA MALLA, TIENE CREADADA SEMESTRES</span>";
                        }
                        if ($this->Pensum_Model->ExisteActividadMatriculas($ide)){
                          $msj_del = $msj_del. "<BR/><span>NO SE PUEDE ELIMINAR  ESTA MALLA, TIENE ASIGNADAS MATRICULAS</span>";
                        }
                        if(empty($msj_del)){
                            $this->Pensum_Model->eliminarPensum($ide);
                            redirect('pensum/inicio');
                        }else{
                            $this->data['msj_del']=$msj_del;
                            $this->data['tit_del']="ELIMINACION DE MALLAS";
                            $this->data['volver']="pensum/inicio";
                            
                            $this->_render('template/msj_error');
                            
                        }
                            
                        
                    }                
                                
                
                
            }
        }
    }

    function presearch() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        
        $who = $this->input->post('txtsearch');

        if (empty($who))
            redirect("pensum/inicio");
        else {
            $this->data['pensum'] = $this->Pensum_Model->getBusquedaPensum($who);
            $this->_render('pensum/start');
        }
    }

    public function ingresodatos() {
      if (!$this->ion_auth->logged_in() || (!$this->ion_auth->is_admin() && !$this->ion_auth->is_secretary())  ) {
            $this->session->set_flashdata('message', 'No pertence al grupo de Docentes');
            redirect('auth/login', 'refresh');
        }
        $this->data['user'] = $this->ion_auth->get_user_username();
        

        $this->load->library('form_validation');
        $this->data['carrera'] = $this->Carrera_Model->getListado();
        $this->form_validation->set_rules('txtdes', 'Descripcion del Pensum', 'required|trim');

        if (($this->form_validation->run() == FALSE)) {

            $this->_render('pensum/new');
        } else {

            $pensum = array(
                "ide_car" => $this->input->post('ide_car'),
                "descripcion_pen" => $this->input->post('txtdes'),
                "estado_pen" => $this->input->post('estado'));

            $this->Pensum_Model->grabarPensum($pensum);
            redirect('pensum/inicio');
        }
    }

}

/* End of file welcome.php */
    /* Location: ./application/controllers/welcome.php */


    